package com.uniview.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.uniview.base.BaseDao;
import com.uniview.entity.po.Menu;
import com.uniview.entity.vo.Pager;
import com.uniview.entity.vo.QueryBuilder;

@Repository("menusDao")
public class MenusDao extends BaseDao<Menu> {

	/**
	 * 新增菜单
	 * 
	 * @param menu
	 *            菜单对象
	 * @return
	 * @throws Exception
	 */
	public int addMenu(Menu menu) throws Exception {
		return (int) this.save(menu);
	}

	/**
	 * 修改菜单
	 * 
	 * @param menu
	 *            菜单对象
	 * @return
	 * @throws Exception
	 */
	public boolean updateMenu(Menu menu) throws Exception {
		return this.update(menu);
	}

	/**
	 * 删除菜单
	 * 
	 * @param mId
	 * @return
	 * @throws Exception
	 */
	public boolean deleteMenu(int mId) throws Exception {
		return this.deleteById(mId);
	}

	/**
	 * 查找所有的菜单
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<Menu> findAllMenu() throws Exception {
		return this.findByHql("FROM Menu where parentId is NULL");
	}

	/**
	 * 查询所有子菜单
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<Menu> findAllClid() throws Exception {
		return this.findByHql("FROM Menu where parentId is NOT NULL");
	}

	/**
	 * 查找菜单下面的所有子菜单
	 * 
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	public List<Menu> findMenuByParentId(int parentId) throws Exception {
		return this.findByHql("FROM Menu where disabled =1 and parentId = ? order by menuSort asc", parentId);
	}

	/**
	 * 根据ID查询菜单信息
	 * 
	 * @param menuId
	 * @return
	 * @throws Exception
	 */
	public Menu findMenuById(int menuId) throws Exception {
		return this.find(menuId);
	}

	/**
	 * 根据菜单类型查询父节点菜单
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<Menu> findParentMenu(Integer menuType) throws Exception {
		String sql = "";
		if (menuType == null) {
			sql = "from Menu m where m.disabled=1 and m.parentId is null order by menuSort asc";
			return this.findByHql(sql);
		} else {
			sql = "from Menu m where m.disabled=1 and m.parentId is null and m.menuType=? order by menuSort asc";
		}
		return this.findByHql(sql, menuType);
	}

	/**
	 * 根据菜单路径查询菜单
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public Menu findMenuByMenuUrl(String url) throws Exception {
		String sql = "select * from un_menus m where m.menuUrl = ?";
		List<Menu> list = this.findBySql(sql, url);
		if (list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 根据roleId查询关联的菜单
	 * 
	 * @param roleId
	 * @return
	 * @throws Exception
	 */
	public List<Menu> findSubMenusByRoleId(Integer roleId) throws Exception {
		String sql = "select * from sd_menus sm where sm.DISABLED=1 and PARENTID is not null and exists(select RMID from sd_resource_role srr where srr.MENUID=sm.MENUID and srr.ROLEID=?)";
		return this.findBySql(sql, roleId);
	}

	/**
	 * 分页查找菜单信息
	 * 
	 * @param queryBuilder
	 * @param pageNo
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public Pager<Menu> findMenuByPage(QueryBuilder queryBuilder, int pageNo, int pageSize) throws Exception {
		return this.findPagerByQueryBuilder(queryBuilder, pageNo, pageSize);
	}

	/**
	 * 根据条件查询总数
	 * 
	 * @param menuName
	 * @param menuId
	 * @return
	 * @throws Exception
	 */
	public Integer findMenuCountByCondition(String menuName, Integer menuId) throws Exception {
		StringBuilder sql = new StringBuilder("select * from sd_menus where 1=1");
		if (StringUtils.isNotBlank(menuName)) {
			sql.append(" and MENUNAME = '" + menuName + "'");
		}
		if (menuId != null && menuId != 0) {
			sql.append(" and MENUID = '" + menuId + "'");
		}
		return this.findBySql(sql.toString()).size();
	}

	/**
	 * 根据用户ID获取对应角色权限的子菜单
	 * 
	 * @param employeeId
	 * @return
	 */
	public List<Menu> findMenuByEmployeeId(String empCode) throws Exception {
		String sql = "select sm.* from sd_menus sm join sd_resource_role srr on sm.menuid=srr.menuid and sm.disabled=1 where srr.roleid"
				+ " in (select sur.roleid from  sd_user_role sur where UPPER(sur.userId)=?)"
				+ " and exists (select sr.id from sd_role sr where sr.id=srr.roleid) order by sm.menusort";
		return this.findBySql(sql, empCode.toUpperCase());
	}

}
